
<template>
  <el-form
    :model="ruleForm"
    status-icon
    :rules="rules"
    ref="ruleForm"
    label-width="100px"
    class="demo-ruleForm"
  >
    <el-form-item
      label="用户名"
      prop="userName"
    >
      <el-input
        type="text"
        v-model="ruleForm.userName"
        autocomplete="off"
      ></el-input>
    </el-form-item>
    <el-form-item
      label="密码"
      prop="passwordMd5"
    >
      <el-input
        type="password"
        v-model="ruleForm.passwordMd5"
        autocomplete="off"
      ></el-input>
    </el-form-item>

    <el-form-item>
      <el-button
        type="primary"
        @click="submitForm('ruleForm')"
      >登录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { login } from "@/api/login.js";
import md5 from "md5";
export default {
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        userName: "",
        passwordMd5: "",
      },
      rules: {
        userName: [{ validator: validatePass, trigger: "blur" }],
        passwordMd5: [{ validator: validatePass, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      console.log(formName);
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let params = { ...this.ruleForm };
          params.passwordMd5 = md5(params.passwordMd5);
          login({
            ...params,
          }).then((data) => {
            console.log(data.data);
            sessionStorage.setItem("token", data.data);
            this.$router.push("/");
            // window.location.href = "http://localhost:8080/introduce";
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang='less'>
</style>
